//
//  PGSideRightView.m
//  Estate
//
//  Created by houzhen on 2017/11/3.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "PGSideRightView.h"

@interface PGSideRightView()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong)UITableView* tableView;
@end
@implementation PGSideRightView
-(instancetype)init{
    self = [super init];
    if(self){
        [self initUI];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self initUI];
    }
    return self;
}
-(void)initUI{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    // 标题
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, DEF_SCREEN_WIDTH-50, 64)];
    [self addSubview:titleView];
    titleView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-15);
        make.centerX.equalTo(titleView.mas_centerX);
    }];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = mainTitleColor;
    titleLabel.text = @"工作";
    
    [self addSubview:self.tableView];
    
    // 点击事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disappear)];
    tapGesture.delegate = self;
    tapGesture.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapGesture];
}


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 如果当前是tableView, 做自己想做的事
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}
-(void)disappear{
    [self hideFromView];
}

-(void)showInView:(UIView*)superView{
    if (superView) {
        [superView addSubview:self];
    }
    _isShowInView = YES;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.frame;
        frame.origin.x = 0;
        self.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)hideFromView{
    
    _isShowInView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.frame;
        frame.origin.x = kScreen_Width;
        self.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.items.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary* itemDic = self.items[section];
    NSArray* itemCount = [itemDic objectForKey:@"items"];
    return itemCount.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* cellid = @"cellid";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    NSDictionary* itemDic = self.items[indexPath.section];
    NSArray* itemCount = [itemDic objectForKey:@"items"];
    cell.textLabel.text = itemCount[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = mainTitleColor;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* itemDic = self.items[indexPath.section];
    NSArray* itemCount = [itemDic objectForKey:@"items"];
    if (self.selectVBlock) {
        self.selectVBlock(itemCount[indexPath.row], indexPath.section, indexPath.row);
    }
    
    [self hideFromView];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 44)];
    headView.backgroundColor = [UIColor clearColor];
    UILabel * headerL = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, kScreen_Width-20, 44)];
    headerL.font = [UIFont systemFontOfSize:15];
    headerL.textColor = mainTitleColor;
    NSDictionary* itemDic = self.items[section];
    headerL.text = [itemDic objectForKey:@"itemStr"];
    [headView addSubview:headerL];
    return headView;
}

-(void)setItems:(NSArray<NSDictionary *> *)items{
    _items = items;
    [self.tableView reloadData];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(50, 64, DEF_SCREEN_WIDTH - 50, DEF_SCREEN_HEIGHT-64)];
        _tableView.backgroundColor = UIColorFromRGB(0xf2f2f2);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = UIColorFromRGB(0xeeeeee);
        _tableView.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

@end
